'use strict';
var P = Object.defineProperty;
var q = (h, e, t) =>
  e in h
    ? P(h, e, { enumerable: !0, configurable: !0, writable: !0, value: t })
    : (h[e] = t);
var o = (h, e, t) => (q(h, typeof e != 'symbol' ? e + '' : e, t), t);
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const j = require('../nodes/Alias.cjs'),
  a = require('../nodes/Collection.cjs'),
  i = require('../nodes/identity.cjs'),
  I = require('../nodes/Pair.cjs'),
  p = require('../nodes/toJS.cjs'),
  C = require('../schema/Schema.cjs'),
  k = require('../stringify/stringifyDocument.cjs'),
  y = require('./anchors.cjs'),
  T = require('./applyReviver.cjs'),
  x = require('./createNode.cjs'),
  w = require('./directives.cjs');
class b {
  constructor(e, t, s) {
    o(this, 'commentBefore', null);
    o(this, 'comment', null);
    o(this, 'contents');
    o(this, 'directives');
    o(this, 'errors', []);
    o(this, 'options');
    o(this, 'warnings', []);
    Object.defineProperty(this, i.NODE_TYPE, { value: i.DOC });
    let n = null;
    typeof t == 'function' || Array.isArray(t)
      ? (n = t)
      : s === void 0 && t && ((s = t), (t = void 0));
    const r = Object.assign(
      {
        intAsBigInt: !1,
        keepSourceTokens: !1,
        logLevel: 'warn',
        prettyErrors: !0,
        strict: !0,
        uniqueKeys: !0,
        version: '1.2',
      },
      s,
    );
    this.options = r;
    let { version: c } = r;
    s != null && s._directives
      ? ((this.directives = s._directives.atDocument()),
        this.directives.yaml.explicit && (c = this.directives.yaml.version))
      : (this.directives = new w.Directives({ version: c })),
      this.setSchema(c, s),
      (this.contents = e === void 0 ? null : this.createNode(e, n, s));
  }
  clone() {
    const e = Object.create(b.prototype, { [i.NODE_TYPE]: { value: i.DOC } });
    return (
      (e.commentBefore = this.commentBefore),
      (e.comment = this.comment),
      (e.errors = this.errors.slice()),
      (e.warnings = this.warnings.slice()),
      (e.options = Object.assign({}, this.options)),
      this.directives && (e.directives = this.directives.clone()),
      (e.schema = this.schema.clone()),
      (e.contents = i.isNode(this.contents)
        ? this.contents.clone(e.schema)
        : this.contents),
      this.range && (e.range = this.range.slice()),
      e
    );
  }
  add(e) {
    l(this.contents) && this.contents.add(e);
  }
  addIn(e, t) {
    l(this.contents) && this.contents.addIn(e, t);
  }
  createAlias(e, t) {
    if (!e.anchor) {
      const s = y.anchorNames(this);
      e.anchor = !t || s.has(t) ? y.findNewAnchor(t || 'a', s) : t;
    }
    return new j.Alias(e.anchor);
  }
  createNode(e, t, s) {
    let n;
    if (typeof t == 'function') (e = t.call({ '': e }, '', e)), (n = t);
    else if (Array.isArray(t)) {
      const D = v =>
          typeof v == 'number' || v instanceof String || v instanceof Number,
        S = t.filter(D).map(String);
      S.length > 0 && (t = t.concat(S)), (n = t);
    } else s === void 0 && t && ((s = t), (t = void 0));
    const {
        aliasDuplicateObjects: r,
        anchorPrefix: c,
        flow: f,
        keepUndefined: u,
        onTagObj: d,
        tag: m,
      } = s ?? {},
      {
        onAnchor: N,
        setAnchors: O,
        sourceObjects: A,
      } = y.createNodeAnchors(this, c || 'a'),
      E = {
        aliasDuplicateObjects: r ?? !0,
        keepUndefined: u ?? !1,
        onAnchor: N,
        onTagObj: d,
        replacer: n,
        schema: this.schema,
        sourceObjects: A,
      },
      g = x.createNode(e, m, E);
    return f && i.isCollection(g) && (g.flow = !0), O(), g;
  }
  createPair(e, t, s = {}) {
    const n = this.createNode(e, null, s),
      r = this.createNode(t, null, s);
    return new I.Pair(n, r);
  }
  delete(e) {
    return l(this.contents) ? this.contents.delete(e) : !1;
  }
  deleteIn(e) {
    return a.isEmptyPath(e)
      ? this.contents == null
        ? !1
        : ((this.contents = null), !0)
      : l(this.contents)
        ? this.contents.deleteIn(e)
        : !1;
  }
  get(e, t) {
    return i.isCollection(this.contents) ? this.contents.get(e, t) : void 0;
  }
  getIn(e, t) {
    return a.isEmptyPath(e)
      ? !t && i.isScalar(this.contents)
        ? this.contents.value
        : this.contents
      : i.isCollection(this.contents)
        ? this.contents.getIn(e, t)
        : void 0;
  }
  has(e) {
    return i.isCollection(this.contents) ? this.contents.has(e) : !1;
  }
  hasIn(e) {
    return a.isEmptyPath(e)
      ? this.contents !== void 0
      : i.isCollection(this.contents)
        ? this.contents.hasIn(e)
        : !1;
  }
  set(e, t) {
    this.contents == null
      ? (this.contents = a.collectionFromPath(this.schema, [e], t))
      : l(this.contents) && this.contents.set(e, t);
  }
  setIn(e, t) {
    a.isEmptyPath(e)
      ? (this.contents = t)
      : this.contents == null
        ? (this.contents = a.collectionFromPath(this.schema, Array.from(e), t))
        : l(this.contents) && this.contents.setIn(e, t);
  }
  setSchema(e, t = {}) {
    typeof e == 'number' && (e = String(e));
    let s;
    switch (e) {
      case '1.1':
        this.directives
          ? (this.directives.yaml.version = '1.1')
          : (this.directives = new w.Directives({ version: '1.1' })),
          (s = { merge: !0, resolveKnownTags: !1, schema: 'yaml-1.1' });
        break;
      case '1.2':
      case 'next':
        this.directives
          ? (this.directives.yaml.version = e)
          : (this.directives = new w.Directives({ version: e })),
          (s = { merge: !1, resolveKnownTags: !0, schema: 'core' });
        break;
      case null:
        this.directives && delete this.directives, (s = null);
        break;
      default: {
        const n = JSON.stringify(e);
        throw new Error(
          `Expected '1.1', '1.2' or null as first argument, but found: ${n}`,
        );
      }
    }
    if (t.schema instanceof Object) this.schema = t.schema;
    else if (s) this.schema = new C.Schema(Object.assign(s, t));
    else
      throw new Error(
        'With a null YAML version, the { schema: Schema } option is required',
      );
  }
  toJS({
    json: e,
    jsonArg: t,
    mapAsMap: s,
    maxAliasCount: n,
    onAnchor: r,
    reviver: c,
  } = {}) {
    const f = {
        anchors: new Map(),
        doc: this,
        keep: !e,
        mapAsMap: s === !0,
        mapKeyWarned: !1,
        maxAliasCount: typeof n == 'number' ? n : 100,
      },
      u = p.toJS(this.contents, t ?? '', f);
    if (typeof r == 'function')
      for (const { count: d, res: m } of f.anchors.values()) r(m, d);
    return typeof c == 'function' ? T.applyReviver(c, { '': u }, '', u) : u;
  }
  toJSON(e, t) {
    return this.toJS({ json: !0, jsonArg: e, mapAsMap: !1, onAnchor: t });
  }
  toString(e = {}) {
    if (this.errors.length > 0)
      throw new Error('Document with errors cannot be stringified');
    if (
      'indent' in e &&
      (!Number.isInteger(e.indent) || Number(e.indent) <= 0)
    ) {
      const t = JSON.stringify(e.indent);
      throw new Error(`"indent" option must be a positive integer, not ${t}`);
    }
    return k.stringifyDocument(this, e);
  }
}
function l(h) {
  if (i.isCollection(h)) return !0;
  throw new Error('Expected a YAML collection as document contents');
}
exports.Document = b;
